/**
 * @Title: TBTHistoryImpl.java
 * @author zengshuai shi
 * @date 2015年9月11日 下午2:56:47
 */
package com.shanghaionstar.sqlite;

import android.content.Context;

import com.onstar.cn.common.util.EncryptUtil;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.sqlite.entities.ModelDestinationHistory;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.exception.DbException;
import com.shanghaionstar.nav.model.PoiHistoryInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zengshuai shi
 * @ClassName: TBTHistoryImpl
 * @Description: TODO
 * @date 2015年9月11日 下午2:56:47
 */
public class TBTHistoryImpl extends BaseSqlImp {
    /**
     * @Description: TODO
     * @author zengshuai shi
     * @date 2015年9月11日 下午3:04:30
     */

    public TBTHistoryImpl(Context c) {
        super(c);
    }

    /**
     * @param @param requestId
     * @param @param historyInfo    设定文件
     * @return void    返回类型
     * @throws
     * @Title: saveModelDestinationHistory
     * @Description: 本地数据库保存/更新记录
     */
    public void saveOrUpdateModelDestinationHistory(String requestId, PoiHistoryInfo historyInfo, boolean isSave) {
        try {
            ModelDestinationHistory p_Info = isSave ? new ModelDestinationHistory() :
                    (ModelDestinationHistory) mDbUtils.findFirst(Selector.from(ModelDestinationHistory.class).where("requestId", "=", EncryptUtil.encrypt(context, requestId)));
            p_Info.setRequestId(EncryptUtil.encrypt(context, requestId));
            if (historyInfo.getPoiItemInfo() != null) {
                p_Info.setTitle(EncryptUtil.encrypt(context, historyInfo.getPoiItemInfo().getTitle()));
                p_Info.setLatitude(String.valueOf(historyInfo.getPoiItemInfo().getLatitude()));
                p_Info.setLongitude(String.valueOf(historyInfo.getPoiItemInfo().getLongitude()));
                p_Info.setAddress(EncryptUtil.encrypt(context, historyInfo.getPoiItemInfo().getAddress()));
                p_Info.setPhone(EncryptUtil.encrypt(context, historyInfo.getPoiItemInfo().getTelphone()));
                p_Info.setCity(EncryptUtil.encrypt(context, historyInfo.getPoiItemInfo().getCityName()));
                p_Info.setCategory(EncryptUtil.encrypt(context, historyInfo.getPoiItemInfo().getTypeDes()));
            }
            p_Info.setStatus(EncryptUtil.encrypt(context, historyInfo.getStatus()));
            p_Info.setMobileLatitude(String.valueOf(historyInfo.getMobileLatitude()));
            p_Info.setMobileLongitude(String.valueOf(historyInfo.getMobileLongitude()));
            p_Info.setSaveTime(String.valueOf(System.currentTimeMillis()));
            if (isSave) {
                mDbUtils.save(p_Info);
                deleteDatabase();
            } else {
                mDbUtils.saveOrUpdate(p_Info);
//                update(p_Info, " requestId=?", new String[]{EncryptUtil.encrypt(context, requestId)});
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

//	public void updateModelDestinationHistory(String requestId,PoiHistoryInfo poiHistoryInfo){
//		ModelDestinationHistory p_Info = new ModelDestinationHistory();
//		p_Info.setRequestId(EncryptUtil.encrypt(context, requestId));
//		p_Info.setTitle(EncryptUtil.encrypt(context, poiHistoryInfo.getPoiItemInfo().getTitle()));
//		p_Info.setLatitude(String.valueOf(poiHistoryInfo.getPoiItemInfo().getLatitude()));
//		p_Info.setLongitude(String.valueOf(poiHistoryInfo.getPoiItemInfo().getLongitude()));
//		p_Info.setAddress(EncryptUtil.encrypt(context, poiHistoryInfo.getPoiItemInfo().getSnippet()));
//		p_Info.setPhone(EncryptUtil.encrypt(context, poiHistoryInfo.getPoiItemInfo().getTelphone()));
//		p_Info.setCity(EncryptUtil.encrypt(context, poiHistoryInfo.getPoiItemInfo().getCityName()));
//		p_Info.setCategory(EncryptUtil.encrypt(context, poiHistoryInfo.getPoiItemInfo().getTypeDes()));
//		p_Info.setStatus(EncryptUtil.encrypt(context, poiHistoryInfo.getStatus()));
//		p_Info.setMobileLatitude(String.valueOf(poiHistoryInfo.getMobileLatitude()));
//		p_Info.setMobileLongitude(String.valueOf(poiHistoryInfo.getMobileLongitude()));
//		p_Info.setSaveTime(String.valueOf(System.currentTimeMillis()));
//		update(p_Info, " requestId=?", new String[] {requestId});
//	}

    /**
     * @return void 返回类型
     * @throws
     * @Title: deleteDatabase
     * @Description: 超过20条删除
     */
    public void deleteDatabase() {
        int id = getId();
        if (id != -1) {
            try {
                mDbUtils.deleteById(ModelDestinationHistory.class,id);
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getId
     * @Description: 获取ID
     */
    public int getId() {
        List<ModelDestinationHistory> list = null;
        try {
            list = mDbUtils.findAll(Selector.from(ModelDestinationHistory.class).orderBy("saveTime",true));
        } catch (DbException e) {
            e.printStackTrace();
        }
//                getAll(null, " 1=1", null, null, null, "saveTime DESC", null);
        if (list != null && list.size() > 20) {
            return list.get(0).getId();
        } else {
            return -1;
        }
    }

    /**
     * @param @return 设定文件
     * @return List<PoiHistoryInfo>    返回类型
     * @throws
     * @Title: getPoiHistoryList
     * @Description: 获取POI本地历史记录
     */
    public List<PoiHistoryInfo> getPoiHistoryList() {
        List<PoiHistoryInfo> historyList = new ArrayList<PoiHistoryInfo>();
        List<ModelDestinationHistory> list = null;
        try {
            list = mDbUtils.findAll(Selector.from(ModelDestinationHistory.class).orderBy("saveTime",true));
        } catch (DbException e) {
            e.printStackTrace();
        }
        for (ModelDestinationHistory model : list) {
            PoiHistoryInfo poiHistoryInfo = new PoiHistoryInfo();
            PoiItemInfo poiItemInfo = new PoiItemInfo();
            poiItemInfo.setTitle(EncryptUtil.decrypt(context, model.getTitle(), ""));
            poiItemInfo.setLatitude(Double.parseDouble(model.getLatitude()));
            poiItemInfo.setLongitude(Double.parseDouble(model.getLongitude()));
            poiItemInfo.setAddress(EncryptUtil.decrypt(context, model.getAddress(), ""));
            poiItemInfo.setTelphone(EncryptUtil.decrypt(context, model.getPhone(), ""));
            poiItemInfo.setCityName(EncryptUtil.decrypt(context, model.getCity(), ""));
            poiItemInfo.setTypeDes(EncryptUtil.decrypt(context, model.getCategory(), ""));
            poiHistoryInfo.setPoiItemInfo(poiItemInfo);
            poiHistoryInfo.setStatus(EncryptUtil.decrypt(context, model.getStatus(), ""));
            poiHistoryInfo.setMobileLatitude(Double.parseDouble(model.getMobileLatitude()));
            poiHistoryInfo.setMobileLongitude(Double.parseDouble(model.getMobileLongitude()));
            poiHistoryInfo.setSaveTime(model.getSaveTime());
            historyList.add(poiHistoryInfo);
        }
        return historyList;
    }

    public void saveHistoryToSql(String requestId, PoiItemInfo poiItemInfo, String status, boolean isSave) {
        PoiHistoryInfo historyInfo = new PoiHistoryInfo();
        historyInfo.setPoiItemInfo(poiItemInfo);
        if (isSave) {
            historyInfo.setStatus("inProgress");
        } else {
            historyInfo.setStatus(status);
        }
        saveOrUpdateModelDestinationHistory(requestId, historyInfo, isSave);
    }
}
